/*
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package net.javlov.example;

import java.awt.Graphics2D;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.geom.AffineTransform;

import javax.swing.JPanel;

public class WorldView extends JPanel implements MouseWheelListener, ActionListener {

	private static final long serialVersionUID = 861818779628522876L;
	
	protected AffineTransform scaleXform;
	protected AffineTransform saveXform;
	protected int modelWidth;
	protected int modelHeight;

	public WorldView() {
		this(1,1);
	}
	
	public WorldView(int w, int h) {
		modelWidth = w; modelHeight = h;
		addMouseWheelListener(this);
		scaleXform = new AffineTransform();
	}
	
	/**
	 * Just invokes paintImmediately to repaint the whole world. Override if anything
	 * more fancy should be going on, e.g. redrawing only the areas that have changed, which
	 * makes sense for worlds with few moving objects.
	 */
	public void draw() {
		paintImmediately(0, 0, getWidth(), getHeight());
	}

	protected void drawOthers(Graphics2D g) {}

	@Override
	public void mouseWheelMoved(MouseWheelEvent e) {
		scaleXform.scale(1 - e.getWheelRotation()*0.05, 1 - e.getWheelRotation()*0.05);
		repaint();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		draw();
	}

	/**
	 * Only call this from within paintComponent, otherwise transform may be an old one
	 * @return
	 */
	protected AffineTransform getOriginalTransform() {
		return saveXform;
	}

	/**
	 * Only call this from within paintComponent, otherwise transform may be an old one
	 * @return
	 */
	protected AffineTransform getScaleTransform() {
		return scaleXform;
	}

}